﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;

public partial class SYS_POPEDOM_Sys_Param_Value_Setting : System.Web.UI.Page
{
    public static string chkedID = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            ViewState["ALLOWMODIFY"] = Request.QueryString["ALLOWMODIFY"];
            ViewState["PARAMETER_ID"]=Request.QueryString["PARAMETER_ID"];
            ViewState["DEPT_ID"] = Request.QueryString["DEPT_ID"];
            ViewState["PARAMETER_STR"] = Request.QueryString["PARAMETER_STR"];
            ViewState["PARAMETER_TYPE"] = 0;
            Hdnrow.Value = Request.QueryString["rowid"];
            DataTable dt = CommonLib.BLL.SYS_PARAMETER.GetList("PARAMETER_ID='" + ViewState["PARAMETER_ID"].ToString() + "'").Tables[0];
            if (dt.Rows.Count>0)
            {    
                ViewState["PARAMETER_TYPE"]=dt.Rows[0]["PARAMETER_TYPE"].ToString();
                if (int.Parse(ViewState["PARAMETER_TYPE"].ToString()) == 1)
                {
                    this.exttxt_Param.Visible = true;
                    if (ViewState["ALLOWMODIFY"].ToString() == "1")
                    {
                        this.exttxt_Param.ReadOnly = true;
                    }
                    this.RadioGroup1.Visible = false;
                    this.exttxt_Param.FieldLabel = dt.Rows[0]["PARAMETER_NAME"].ToString();
                    ShowTxtValue(ViewState["DEPT_ID"].ToString(), ViewState["PARAMETER_ID"].ToString());
                }
                else
                {
                    this.exttxt_Param.Visible = false;
                    this.RadioGroup1.Visible = true;
                    string[] rdiArray = ParseSelectStr(dt.Rows[0]["PARAMETER_OPTION"].ToString());
                    if (rdiArray == null)
                    {
                        this.FormPanel1.Visible = false;
                    }
                    else
                    {
                        this.RadioGroup1.FieldLabel = dt.Rows[0]["PARAMETER_NAME"].ToString();
                        if (ViewState["ALLOWMODIFY"].ToString() == "1")
                        { 
                            LoadRadioItems(rdiArray,true);
                        }
                        else
                        {
                            LoadRadioItems(rdiArray, false);
                        }
                       
                    }

                    ShowRadioCheck(ViewState["DEPT_ID"].ToString(), ViewState["PARAMETER_ID"].ToString());
                }
            }
        }
    }


    //解析选项字符串

    private string[] ParseSelectStr(string strSelect)
    {
        string[] stringSeparators = new string[] { "@" };

        string[] sArray = strSelect.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);

        return sArray;

    }

    //加载选项
    private void LoadRadioItems(string[] rdiArray,bool onlyread)
    {
       
        foreach (string sOne in rdiArray)
        {
            if (sOne.IndexOf(':') > 0)
            {
                char[] charSeparators = new char[] { ':' };
                string[] sItem = sOne.Split(charSeparators);
                Ext.Net.Radio rdi = new Radio();
                rdi.ID = sItem[0];
                rdi.BoxLabel = sItem[1];
                rdi.Checked = false;
                if (onlyread)
                    rdi.ReadOnly = true;
                this.RadioGroup1.Items.Add(rdi);
            }
        }
    }

    //显示选中选项
    private void ShowRadioCheck(string deptid,string paramid)
    {
        if (this.RadioGroup1.Items.Count > 0)
        {
            {
                for (int i = 0; i < this.RadioGroup1.Items.Count; i++)
                {
                    if (this.RadioGroup1.Items[i].ID == ViewState["PARAMETER_STR"].ToString())
                    {
                        this.RadioGroup1.Items[i].Checked = true;
                    }
                }
            }
        }
    }

    //显示参数值

    private void ShowTxtValue(string deptid, string paramid)
    {
        this.exttxt_Param.Text = ViewState["PARAMETER_STR"].ToString();
    }

}
